﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanhTu.GNCMedia.KQXS.DataAccess.DO;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using ThanhTu.GNCMedia.KQXS.Common;

namespace ThanhTu.GNCMedia.KQXS.DataAccess.DAL
{
    public class RegionDAL : BaseDAL
    {
        public RegionDAL() : base() { }

        public int add(RegionD0 vRegionD0)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "Region_PKG.SaveRegion";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
            oraParam.Value = vRegionD0.RegionID;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("INREGION_NAME", OracleDbType.Varchar2);
            oraParam.Value = vRegionD0.RegionName;
            oraComm.Parameters.Add(oraParam);


            oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            int result = ExecuteNoneQuery(oraComm);

            if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
            { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return result;
        }

        public int delete(string ID_Region)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "Region_PKG.DeleteRegion";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
            oraParam.Value = ID_Region;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            int result = ExecuteNoneQuery(oraComm);

            if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
            { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return result;
        }
        public RegionD0 getbyRegion(string IDRegion)
        {
            OracleCommand oraComm = new OracleCommand();
            oraComm.CommandType = CommandType.StoredProcedure;
            oraComm.CommandText = "Region_PKG.GetByRegionPK";
            OracleParameter oraParam = null;

            oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
            oraParam.Value = IDRegion;
            oraComm.Parameters.Add(oraParam);

            oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
            oraParam.Direction = ParameterDirection.Output;
            oraComm.Parameters.Add(oraParam);

            DataSet ds = GetDataSet(oraComm);
            DataRow dr = null;
            RegionD0 vRegionD0 = null;

            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dr = ds.Tables[0].Rows[0];
                        vRegionD0 = new RegionD0();

                        if (!Convert.IsDBNull(dr["REGIONID"]))
                        { vRegionD0.RegionID = Convert.ToString(dr["REGIONID"]); }

                        if (!Convert.IsDBNull(dr["REGIONNAME"]))
                        { vRegionD0.RegionID =  Utils.ToUTF8String((byte[])dr["REGIONNAME"]) ; }
                        dr = null;
                    }
                }

                ds.Dispose();
                ds = null;
            }

            oraComm.Dispose();
            oraComm = null;
            oraParam = null;

            return vRegionD0;
        }
        public List<RegionD0> SelectALL()
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "Region_PKG.GetAllRegionNonePage";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;
                    List<RegionD0> vRegionD0Collection = null;
                    DataRow dr = null;
                    RegionD0 vRegionD0 = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                            vRegionD0Collection = new List<RegionD0>();

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dr = dt.Rows[i];
                                vRegionD0 = new RegionD0();

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vRegionD0.RegionID = Convert.ToString(dr["REGIONID"]); }

                                if (!Convert.IsDBNull(dr["REGIONNAME"]))
                                { vRegionD0.RegionName = Utils.ToUTF8String((byte[])dr["REGIONNAME"]); }

                                vRegionD0Collection.Add(vRegionD0);
                                dr = null;
                                vRegionD0 = null;
                            }

                            dt.Dispose();
                            dt = null;
                        }
                    }
                    return vRegionD0Collection;
                }
            }
        }
    }
}
